package com.xuxin.baby.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.ServiceBean;
import com.xuxin.baby.contract.ServiceContract;
import com.xuxin.baby.presenter.ServicePresenter;
import com.xuxin.baby.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.05 0005
 * 客服
 */
public class ServiceActivity extends BaseActivity implements ServiceContract.View {
    private final ServiceContract.Presenter presenter = new ServicePresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.service_phone)
    TextView service_phone;

    private String tenantId;//租户id
    private String token;//授权token
    private String telphone;//客服电话

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_service);
    }

    @Override
    public void initView() {
        title_name.setText("平台客服");
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(context);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.service_call})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.service_call) {
            try {
                if (!StringUtils.isEmpty(telphone)) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + telphone);
                    intent.setData(data);
                    startActivity(intent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getService(tenantId, token);
            }
            return false;
        }
    });


    @Override
    public void onSuccess(ServiceBean serviceBean) {
        telphone = serviceBean.getResult().getTelphone();
        service_phone.setText(telphone);
    }
}